package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.web.admin.mapper.ApartmentFeeValueMapper;
import com.atguigu.lease.web.admin.service.FileService;

import org.joda.time.DateTime;
import org.springframework.util.StringUtils;
import io.minio.*;
import io.minio.errors.*;
import lombok.SneakyThrows;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private MinioClient minioClient;


    @Autowired
    private MinioProperties minioProperties;

    @SneakyThrows
    @Override
    public String upload(MultipartFile file)  {
        InputStream inputStream = file.getInputStream();// 图片的流
        // 客户端
        MinioClient minioClient1 = MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();

        // 文件和路径和后缀

        String originalFilename = file.getOriginalFilename();
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);//后缀
        String uuname = UUID.randomUUID().toString().replace("-", "");//路径
        String dateStr = new DateTime().toString("yyyy/MM/dd/");//文件名
        String fileName = dateStr + uuname + "." + filenameExtension;

        // 参数
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .stream(inputStream,file.getSize(),-1)
                .bucket(minioProperties.getBucketName())
                .object(fileName)// 日期文件夹路径+UUID文件名+后缀
                .build();

        // 上传
        minioClient.putObject(putObjectArgs);


        return minioProperties.getEndpoint() + "/"+ minioProperties.getBucketName()+ "/"+ fileName;
    }

//    @Override
//    public String upload(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
//
//           boolean bucketExists = minioClient.bucketExists(BucketExistsArgs
//                           .builder()
//                           .bucket(minioProperties.getBucketName())
//                           .build());
//            if (!bucketExists){
//                minioClient.makeBucket(MakeBucketArgs
//                        .builder()
//                        .bucket(minioProperties.getBucketName())
//                        .build());
//                minioClient.setBucketPolicy(SetBucketPolicyArgs
//                        .builder()
//                        .bucket(minioProperties.getBucketName())
//                        .config(createBucketPolicyConfig(minioProperties.getBucketName()))
//                        .build());
//            }
//           String filename = new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + UUID.randomUUID() + "-" + file.getOriginalFilename();
//            minioClient.putObject(PutObjectArgs
//                    .builder()
//                    .bucket(minioProperties.getBucketName())
//                    .stream(file.getInputStream(),file.getSize(),-1)
//                    .object(filename)
//                    .contentType(file.getContentType())
//                    .build());
//            return String.join("/",minioProperties.getEndpoint(),minioProperties.getBucketName(),filename);
//
//    }
//    private String createBucketPolicyConfig(String bucketName) {
//
//        return """
//            {
//              "Statement" : [ {
//                "Action" : "s3:GetObject",
//                "Effect" : "Allow",
//                "Principal" : "*",
//                "Resource" : "arn:aws:s3:::%s/*"
//              } ],
//              "Version" : "2012-10-17"
//            }
//            """.formatted(bucketName);
//    }
}
